package com.justspeak.util.io;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.LinkedList;
import java.util.List;
import android.os.Environment;
/** 
 * copyright    Founder 2010
 * @date        2011-9-15 V1.0初始作成
 * @author      Administrator
 * @version     V1.0
 * @brief       xxxClass的定义
 * @since       1.0
 * @history     2011-9-15 V1.X 修改内容，一个版本一行
 * @descript    类的概要说明(需要手动修改)
 * @todo        未完成的事项
 */
public class FileUtils
{
    private String sdCardRoot;
        
        public FileUtils(){
            sdCardRoot=Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator;
        }
        
        public File createFileInSdCard(String fileName, String dir) throws IOException{
            File file=new File(sdCardRoot+dir+File.separator+fileName);
            file.createNewFile();
            return file;
        }
    
        public File createSdDir(String dir){
            File dirFile=new File(sdCardRoot+dir+File.separator);
            dirFile.mkdir();
            return dirFile;
        }
        
        public boolean isFileEixt(String fileName,String dir){
            File file=new File(sdCardRoot+dir+File.separator+fileName);
            return file.exists();
        }
        
        public File write2SdFromInput(String path,String fileName,InputStream input){
            File file=null;
            OutputStream outputStream=null;
            createSdDir(path);
            try {
                file=createFileInSdCard(fileName, path);
                outputStream=new FileOutputStream(file);
                byte buffer[]=new byte[4*1024];
                int temp;
                while ((temp=input.read(buffer))!=-1) {
                    outputStream.write(buffer, 0, temp);
                }
                outputStream.close();
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally{
                try {
                    outputStream.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            return file;
        }
}
